<?php
namespace app\wxapi\controller;
use think\Controller;
use think\Request;
use think\Db;
class Index extends Controller
{	
	
    public function index()
    {
		print_r($result);
    }
	
	//获取分类
	public function get_cateLst(Request $request){
		if($request->isPost()){
			$where = [
				'follow_wxapi' => 1
			];
			$cate = Db::name('navigate')->where($where)->select();
			return json_encode(['code'=>0,'msg'=>'success','cate'=>$cate]);
		}
		return json_encode(['code'=>2,'msg'=>'网络请求错误']);
	}
	
	//获取内容
	public function get_contentLst(Request $request){
		if($request->isPost()){
			$cid = $request->param('cid');
			if($cid){
				$where = [
					'cid' => $request->param('cid'),
					'is_delete' => 1
				];
				$content = Db::name('table_article')->where($where)->select();
				$list = [];
				foreach($content as $key => $val){
					$val['updatetime'] = date('Y-m-d H:i:s',$val['updatetime']);
					$val['issuetime'] = date('Y-m-d H:i:s',$val['issuetime']);
					$list[] = $val;
				}
				return json_encode(['code'=>0,'msg'=>'success','content'=>$list]);
			}else{
				return json_encode(['code'=>3,'msg'=>'请求参数有误']);
			}
		}
		return json_encode(['code'=>2,'msg'=>'网络请求错误']);
	}
	
	//获取文章详情
	public function get_ArticleDetail(Request $request){
		if($request->isPost()){
			$articleId = $request->param('articleid');
			if($articleId){
				$where = [
					'art_id' => $articleId
				];
				$queryDetail = Db::name('table_article')->where($where)->find();
				if($queryDetail){
					return json_encode(['code'=>0,'msg'=>'success','data'=>$queryDetail]);
				}else{
					return json_encode(['code'=>0,'msg'=>'success','data'=>[]]);
				}
			}else{
				return json_encode(['code'=>3,'msg'=>'请求参数有误']);
			}
		}else{
			return json_encode(['code'=>502,'msg'=>'请求错误']);
		}
	}
	
	//搜索
	public function searchContent(Request $request){
		if($request->isPost()){
			$keyword = $request->param('keyword','');
			if($keyword){
				//按条件搜索
				$where['title|content'] = ['like', '%' . $keyword . '%'];
				$content = Db::name('table_article')->where($where)->select();
				// $count = Db::name("table_article")->where('title','like',"%".$keyword."%")->whereOr('content','like',"%".$keyword."%")->count();
			}else{
				//搜索全部
				$sql = "SELECT * FROM my_table_article left join my_navigate on my_table_article.cid = my_navigate.navigate_id where my_navigate.follow_wxapi = 1";
				$content = Db::query($sql);
			}
			$list = [];
			foreach($content as $key => $val){
				$val['updatetime'] = date('Y-m-d H:i:s',$val['updatetime']);
				$val['issuetime'] = date('Y-m-d H:i:s',$val['issuetime']);
				$list[] = $val;
			}
			return json_encode(['code'=>0,'msg'=>'success','data'=>$list]);
		}else{
			return json_encode(['code'=>2,'msg'=>'网络请求错误']);
		}
	}
}
